package com.gzhryc.shared_device.oem.h5.mng.controllers.agent;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentType;
import com.gzhryc.shared_device.oem.code.agent.dao.models.AgentTypeCount;
import com.gzhryc.shared_device.oem.code.agent.dao.models.AgentTypeUser;
import com.gzhryc.shared_device.oem.code.agent.services.AgentTypeRelService;
import com.gzhryc.shared_device.oem.code.agent.services.AgentTypeService;
import com.gzhryc.shared_device.oem.code.agent.services.dto.AgentTypeSearch;

public class AgentTypeController {

    @Route(value = { "/agent/type/list.html" })
    public String list(HttpServletRequest request) {
        return "/agent/type/list.html";
    }

    @Route(value = "/agent/type/list.data")
    public ListResult<AgentType> listData(HttpServletRequest request) {
        ListResult<AgentType> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        AgentTypeSearch search = new AgentTypeSearch();
        result.getBody().setData(AgentTypeService.self().findBySearch(search));
        if (result.getBody().getData() != null) {
        	result.setCode(DtoConstants.SUCCESS,null);
        	List<Long> typeIds = new ArrayList<Long>();
        	for(AgentType type : result.getBody().getData()) {
        		typeIds.add(type.getId());
        	}
        	
        	List<AgentTypeCount> data = AgentTypeRelService.self().findCountByTypeIds(typeIds);
        	if(data != null && data.size() > 0) {
        		for(AgentType type : result.getBody().getData()) {
        			for(AgentTypeCount item : data) {
        				if(type.getId().equals(item.getTypeId())) {
        					type.setAgentCount(item.getUseCount());
        				}
        			}
            	}
        	}
        }
        return result;
    }

    @Route(value = { "/agent/type/add.html" })
    public String add(HttpServletRequest request) {
        return "/agent/type/edit.html";
    }

    @Route(value = { "/agent/type/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增失败，请稍后再试");
        AgentType agentType = WebUtils.getParameter(request, AgentType.class);
        try {
            if(AgentTypeService.self().add(agentType)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/agent/type/info.html" })
    public String info(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        if(id != null){
            AgentType agentType = AgentTypeService.self().getById(id);
            WebUtils.setAttribute(request,agentType);
        }
        return "/agent/type/info.html";
    }

    @Route(value = { "/agent/type/update.do" })
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"修改失败，请稍后再试");
        AgentType agentType = WebUtils.getParameter(request, AgentType.class);
        try {
            if(AgentTypeService.self().updateNotNull(agentType)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = "/agent/type/user/list.data")
    public ListResult<AgentTypeUser> agentListData(HttpServletRequest request){
    	 ListResult<AgentTypeUser> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
    	 Long typeId = WebUtils.getLong(request, "typeId");
         result.getBody().setData(AgentTypeRelService.self().findJoinByTypeId(typeId));
         if (result.getBody().getData() != null) {
             result.setCode(DtoConstants.SUCCESS,null);
         }
         return result;
    }
    
    @Route(value = { "/agent/type/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败，请稍后再试");
        Long id = WebUtils.getLong(request,"id");
        try {
            if(AgentTypeService.self().deleteById(id)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
